package com.mujin.keji.collectionsystem.module.tripplan.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.route.MassTransitRouteLine;
import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.listeners.CustomItemClickListener;
import com.mujin.keji.collectionsystem.module.tripplan.dto.MassTransitRouteLineDto;
import com.mujin.keji.collectionsystem.utils.DateUtil;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 公交路线规划具体详情
 *
 * @author weishixiong
 * @Time 2018-04-10
 */

public class BusPlanDetailAdapter extends RecyclerView.Adapter<BusPlanDetailAdapter.ViewHolder> {
    private static final String TAG = "BusPlanDetailAdapter";
    private Activity mContext;
    private List<List<MassTransitRouteLine.TransitStep>> datas = new ArrayList<>();
    private CustomItemClickListener listener;
    String startAdress;
    String stopAdress;
    List<MassTransitRouteLine.TransitStep> a = new ArrayList<>();

    public BusPlanDetailAdapter(String startAdress, String stopAdress, Activity context, List<List<MassTransitRouteLine.TransitStep>> datas, CustomItemClickListener listener) {
        this.datas.clear();
        this.datas.add(a);
        mContext = context;
        this.datas.addAll(datas);
        this.datas.add(a);
        this.listener = listener;
        this.startAdress = startAdress;
        this.stopAdress = stopAdress;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bus_plan_detail, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position == 0) {

            holder.ivLineStart.setVisibility(View.GONE);
            holder.ivLineStop.setVisibility(View.VISIBLE);

            holder.tvStartStop.setText(startAdress);

            holder.llTop.setVisibility(View.VISIBLE);
            holder.llCenter.setVisibility(View.GONE);
            holder.rlBottom.setVisibility(View.GONE);

        } else if (position == datas.size() - 1) {

            holder.tvStartStop.setText(stopAdress);
            holder.ivLineStart.setVisibility(View.VISIBLE);
            holder.ivLineStop.setVisibility(View.GONE);

            holder.llTop.setVisibility(View.VISIBLE);
            holder.llCenter.setVisibility(View.GONE);
            holder.rlBottom.setVisibility(View.GONE);

        } else {
            try {
                //介绍
                String instructions = datas.get(position).get(0).getInstructions();
                if (EmptyUtils.isNotEmpty(datas.get(position).get(0).getBusInfo())) {

                    //1 地铁    //0 公交
                    holder.llTop.setVisibility(View.GONE);
                    holder.llCenter.setVisibility(View.GONE);
                    holder.rlBottom.setVisibility(View.VISIBLE);
                    //起点
                    holder.tvBusStart.setText(datas.get(position).get(0).getBusInfo().getDepartureStation());

                    for (int i = 0; i < datas.get(position).size(); i++) {
                        if (i == 0) {
                            holder.tvLine.setText(datas.get(position).get(i).getBusInfo().getName());
                        } else {
                            //多少路
                            holder.tvLine.setText(holder.tvLine.getText().toString() + " / " + datas.get(position).get(i).getBusInfo().getName());
                        }
                    }

                    //终点
                    holder.tvBusStop.setText(datas.get(position).get(0).getBusInfo().getArriveStation());
                    //多少站
                    holder.tvStation.setText(datas.get(position).get(0).getBusInfo().getStopNum() + "站");
                    //花费时间
                    holder.tvTime.setText("(" + DateUtil.minForDay(datas.get(position).get(0).getDuration() / 60) + ")");

                    int stopIndex = 0;
                    int startIndex = 0;
                    if (instructions.contains("方向") && instructions.contains("(") && instructions.contains(")")) {
                        String[] directionList = instructions.split("()");
                        for (int i = 0; i < directionList.length; i++) {
                            if ("方".equals(directionList[i]) && "向".equals(directionList[i + 1])) {
                                stopIndex = i + 1;
                                break;
                            }
                        }
                        for (int i = stopIndex; i > 0; i--) {
                            if ("(".equals(directionList[i])) {
                                startIndex = i;
                                break;
                            }
                        }
                        holder.tvBusDerection.setText(instructions.substring(startIndex, stopIndex));
                        holder.tvBusDerection.setVisibility(View.VISIBLE);
                    } else {
                        holder.tvBusDerection.setVisibility(View.GONE);
                    }
                    datas.get(position).get(0).getTrafficConditions();
                } else {
                    if (instructions.contains("步行") && instructions.contains("米")) {
                        //步行
                        holder.llTop.setVisibility(View.GONE);
                        holder.llCenter.setVisibility(View.VISIBLE);
                        holder.rlBottom.setVisibility(View.GONE);
                        //提取文字中的数字
                        String regEx = "[^0-9]";
                        Pattern p = Pattern.compile(regEx);
                        Matcher m = p.matcher(instructions);
                        holder.tvWalk.setText("步行" + Integer.parseInt(m.replaceAll("").trim()) + "米" + "(" + DateUtil.minForDay(datas.get(position).get(0).getDuration() / 60) + ")");
                    }

                }
            } catch (Exception e) {
                //Logger.d(TAG, "onBindViewHolder: " + e);
            }

        }


    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvWalk;
        private TextView tvBusStart;
        private TextView tvLine;
        private TextView tvBusDerection;
        private TextView tvStation;
        private TextView tvTime;
        private TextView tvBusStop;
        private TextView tvStartStop;
        private View ivLineStart;
        private View ivLineStop;
        private RelativeLayout llTop;
        private LinearLayout llCenter;
        private RelativeLayout rlBottom;


        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);

            tvWalk = (TextView) itemView.findViewById(R.id.tv_walk);
            tvBusStart = (TextView) itemView.findViewById(R.id.tv_bus_start);
            tvLine = (TextView) itemView.findViewById(R.id.tv_line);
            tvBusDerection = (TextView) itemView.findViewById(R.id.tv_bus_derection);
            tvStation = (TextView) itemView.findViewById(R.id.tv_station);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvBusStop = (TextView) itemView.findViewById(R.id.tv_bus_stop);
            tvStartStop = (TextView) itemView.findViewById(R.id.tv_start_stop);
            llTop = (RelativeLayout) itemView.findViewById(R.id.ll_top);
            ivLineStart = (View) itemView.findViewById(R.id.iv_line_start);
            ivLineStop = (View) itemView.findViewById(R.id.iv_line_stop);
            llCenter = (LinearLayout) itemView.findViewById(R.id.ll_center);
            rlBottom = (RelativeLayout) itemView.findViewById(R.id.rl_bottom);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });
        }

    }
}
